package com.hhu.wangzb.common.security;

import com.hhu.wangzb.common.security.constant.SecurityConstants;

import java.io.Serializable;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 存储认证信息
 */
public class AuthenticationInfo implements Serializable {

    private Map<String, Object> infoMap = new LinkedHashMap<>();

    public AuthenticationInfo() {

    }

    public AuthenticationInfo(Integer userId, String userRole, String... userPermissions) {
        this.infoMap.put(SecurityConstants.S_SESSION_USER_ID, userId);
        this.infoMap.put(SecurityConstants.S_SESSION_USER_ROLE, userRole);
        this.infoMap.put(SecurityConstants.S_SESSION_USER_PERMISSIONS, Arrays.asList(userPermissions));
    }

    public AuthenticationInfo(Integer userId, String userRole, List<String> userPermissions) {
        this.infoMap.put(SecurityConstants.S_SESSION_USER_ID, userId);
        this.infoMap.put(SecurityConstants.S_SESSION_USER_ROLE, userRole);
        this.infoMap.put(SecurityConstants.S_SESSION_USER_PERMISSIONS, userPermissions);
    }

    public Integer getUserIdInSession() {
        return (Integer) this.infoMap.get(SecurityConstants.S_SESSION_USER_ID);
    }

    public String getUserRoleInSession() {
        return (String) this.infoMap.get(SecurityConstants.S_SESSION_USER_ROLE);
    }

    public List<String> getUserPermissionsInSession() {
        return (List<String>) this.infoMap.get(SecurityConstants.S_SESSION_USER_PERMISSIONS);
    }

    public Map<String, Object> getInfoMap() {
        return infoMap;
    }

//    /**
//     * 当前登陆信息是否表示已认证
//     * @return
//     */
//    public boolean isAuthenticated() {
//        if (infoMap.size() == 0) return false;
//
//        for (String key : infoMap.keySet()) {
//            if (infoMap.get(key) == null) return false;
//        }
//
//        return true;
//    }
}
